package com.xts.weinfo.ui.adapter;

import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentStatePagerAdapter;

import com.xts.weinfo.base.BaseFragment;
import com.xts.weinfo.bean.DataBean;
import com.xts.weinfo.bean.ItInfoTabBean;

import java.util.ArrayList;
import java.util.List;

/**
 * 在FragmentManager中是否有缓存,fragment生命周期也不一样
 * FragmentPagerAdapter:onPause->onStop->onDestroyView
 * FragmentStatePagerAdapter: 销毁的生命周期:onPause->onStop->onDestroyView->onDestroy->onDetach(完全销毁了)
 */
public class VpItInfoAdapter extends FragmentStatePagerAdapter {
    private final List<DataBean> mTabs;
    private final ArrayList<BaseFragment> mFragments;
    private final ArrayList<String> mTitles;

    public VpItInfoAdapter(FragmentManager fm, List<DataBean> tabs, ArrayList<BaseFragment> fragments) {
        super(fm);
        //既有感兴趣的公众号,也有不感兴趣的
        mTabs = tabs;
        mFragments = fragments;

        //感兴趣公众号的标题
        mTitles = new ArrayList<>();
        for (int i = 0; i < mTabs.size(); i++) {
            DataBean dataBean = mTabs.get(i);
            if (dataBean.getIsInterested()){
                mTitles.add(dataBean.getName());
            }
        }
    }

    @Override
    public Fragment getItem(int position) {
        return mFragments.get(position);
    }

    @Override
    public int getCount() {
        return mFragments.size();
    }

    @Nullable
    @Override
    public CharSequence getPageTitle(int position) {
        return mTitles.get(position);
    }
}
